package net.csdra.libraryborrowing.service.user;

import net.csdra.libraryborrowing.domain.user.User;
import net.csdra.libraryborrowing.domain.user.UserRepository;
import net.csdra.libraryborrowing.util.Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.nio.file.AccessDeniedException;

/**
 * Created by msf on 2017/10/11.
 */
@Service
public class UserServiceImpl implements UserService {

    CurrentUserInfo currentUserInfo;

    UserRepository userReposiotry;

    PasswordEncoder passwordEncoder;

    @Autowired
    public UserServiceImpl(CurrentUserInfo currentUserInfo, UserRepository userReposiotry, PasswordEncoder passwordEncoder) {
        this.currentUserInfo = currentUserInfo;
        this.userReposiotry = userReposiotry;
        this.passwordEncoder = passwordEncoder;
    }


    @Override
    public User weiXinResetPassword(WeiXinResetPassword resetPassword) {
        Assert.notNull(resetPassword, "@参数不能为空");
        Assert.hasLength(resetPassword.getUserName(), "@用户名不能为空");
        Assert.hasLength(resetPassword.getCurrentOpenId(), "@微信Id不能为空");
        User user = this.userReposiotry.findByOpenIdAndUserName(resetPassword.getUserName(), resetPassword.getCurrentOpenId());
        Assert.state(resetPassword.getNewPassword() != null && resetPassword.getNewPassword().length() > 1, "@密码不能为空");
        String passwrod = passwordEncoder.encode(resetPassword.getNewPassword());
        Assert.notNull(user, "@用户不存在");
        user.setPassword(passwrod);
        userReposiotry.save(user);
        User result = new User();
        BeanUtils.copyProperties(user, result);
        return result;
    }

    @Override
    public User login(Login login) {
        Assert.notNull(login, "@参数不能为空");
        Assert.hasLength(login.getUserName(), "@用户名不能为空");
        Assert.state(login.getPassword() != null && login.getPassword().length() > 0, "@密码不能为空");
        String password = passwordEncoder.encode(login.getPassword());
        User user = this.userReposiotry.findByUserNameAndPassword(login.getUserName(), password);
        Assert.notNull(user, "@用户或密码错误");
        currentUserInfo.set(user);
        return user;
    }

    @Override
    public User weiXinLogin(WeiXinLogin weiXinLogin) {
        Assert.notNull(weiXinLogin, "参数不能为空");
        Assert.hasLength(weiXinLogin.getOpenId(), "参数不能为空");
        User user = this.userReposiotry.findByOpenId(weiXinLogin.getOpenId());
        Assert.notNull(user, "@未绑定对应账户");
        currentUserInfo.set(user);
        return user;
    }

    @Override
    public User updateUser(UpdateUser updateUser) throws AccessDeniedException {
        Assert.notNull(updateUser, "@参数不能为空");
        String currentUserId = currentUserInfo.get().getId();
        if (updateUser.getPassword() != null && updateUser.getPassword().length() > 0) {
            String password = passwordEncoder.encode(updateUser.getPassword());
            updateUser.setPassword(password);
        }
        User user = userReposiotry.findOne(currentUserId);
        Assert.notNull(user, "@修改对象不存在");
        Utils.copy(updateUser, user);
        userReposiotry.save(user);
        User result = new User();
        Utils.copy(user, result);
        return result;

    }

    @Override
    public User bindUserOpenId(BindUserOpenId bindUserOpenId) {
        Assert.notNull(bindUserOpenId, "@参数不能为空");
        Assert.hasLength(bindUserOpenId.getUserName(), "@用户名不能为空");
        Assert.hasLength(bindUserOpenId.getOpenId(), "@openId不能为空");
        Assert.state(bindUserOpenId.getPassword() != null && bindUserOpenId.getPassword().length() > 0, "@密码不能为空");
        String password = passwordEncoder.encode(bindUserOpenId.getPassword());
        User user = userReposiotry.findByUserNameAndPassword(bindUserOpenId.getUserName(), password);
        Assert.notNull(user, "@用户或密码错误");
        Assert.state(user.getOpenId().equals(user.getUserName()), "@此用户已经绑定微信号");
        user.setOpenId(bindUserOpenId.getOpenId());
        userReposiotry.save(user);
        User result = new User();
        Utils.copy(user, result);
        return result;
    }

    @Override
    public User unBindUserOpenId(UnBindUserOpenId unBindUserOpenId) {
        Assert.notNull(unBindUserOpenId, "@参数不能为空");
        Assert.hasLength(unBindUserOpenId.getUserName(), "@用户名不能为空");
        Assert.state(unBindUserOpenId.getPassword() != null && unBindUserOpenId.getPassword().length() > 0, "@密码不能为空");
        String password = passwordEncoder.encode(unBindUserOpenId.getPassword());
        User user = userReposiotry.findByUserNameAndPassword(unBindUserOpenId.getUserName(), password);
        Assert.notNull(user, "@操作对象不存在");
        //TODO
        user.setOpenId(user.getUserName());
        userReposiotry.save(user);
        User resutl = new User();
        Utils.copy(user, resutl);
        return resutl;
    }

    @Override
    public UserForm getUserById(UserManageService.GetUserById getUserById) {
        Assert.notNull(getUserById, "@参数不能为空");
        Assert.hasLength(getUserById.getId(), "@id不能为空");
        User user = userReposiotry.findOne(getUserById.getId());
        UserForm result = new UserForm();
        Utils.copy(user, result);
        return result;
    }
}
